package com.neusoft.userserver.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.userserver.domain.RespBean;
import com.neusoft.userserver.domain.UserDTO;
import com.neusoft.userserver.entity.Role;
import com.neusoft.userserver.entity.Users;
import com.neusoft.userserver.holder.LoginUserHolder;
import com.neusoft.userserver.service.IRoleService;
import com.neusoft.userserver.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    IUsersService iUsersService;
    @Autowired
    IRoleService iRoleService;
    @Autowired
    private LoginUserHolder loginUserHolder;

    @GetMapping("/currentUser")
    public RespBean currentUser() {

        UserDTO currentUser = loginUserHolder.getCurrentUser();
        Users users = iUsersService.getById(currentUser.getId());
        currentUser.setUserDeptId(users.getUserDeptId());
        currentUser.setUserRealname(users.getUserRealname());
        return RespBean.ok(200, "", currentUser);
    }

    @GetMapping("/getUserById")
    public RespBean getUserById(int id) {
        Users users = iUsersService.getById(id);
        return RespBean.ok(200, "", users);
    }

    @PostMapping("/getUsersByIds")
    public RespBean getUsersByIds(@RequestBody List<Integer> ids) {
        List<Users> users = iUsersService.listByIds(ids);
        return RespBean.ok(200, "get success", users);
    }

    @GetMapping("/getUserByUsername")
    public Users getUserByUsername(String username) {
        return iUsersService.getOne(Wrappers.<Users>lambdaQuery().eq(Users::getUserName, username));
    }

    @GetMapping("/getRolesByName")
    public List<Role> getUserRoles(String username) {
        return iRoleService.getRolesByName(username);
    }

    @GetMapping("/getCustomerManagerByDeptId")
    public RespBean getCustomerManagerByDeptId(String deptId) {
        List<Users> allCustomerManager = iUsersService.getAllCustomerManager();
        List<Users> list = new ArrayList<>();
        for (Users users : allCustomerManager) {
            if (users.getUserDeptId().equals(deptId)) {
                list.add(users);
            }
        }

        return RespBean.ok(200, "get success", list);
    }

    @GetMapping("/getAllCustomerManager")
    public RespBean getAllCutsomerManager() {
        List<Users> allCustomerManager = iUsersService.getAllCustomerManager();
        return RespBean.ok(200, "get success", allCustomerManager);
    }
}
